package com.timmy.cloud_pc_test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private View rootView;

    private int lastDownMouseKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootView = findViewById(R.id.root_view);

        rootView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    rootView.requestPointerCapture();
                }
            }
        }, 1000);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.rootView.setFocusable(true);
            this.rootView.requestFocus();
            this.rootView.setFocusableInTouchMode(true);
            this.rootView.requestFocusFromTouch();
            this.rootView.setDefaultFocusHighlightEnabled(false);

            rootView.setOnCapturedPointerListener(new View.OnCapturedPointerListener() {
                @Override
                public boolean onCapturedPointer(View view, MotionEvent event) {
                    int buttonState = event.getButtonState();
                    int action = event.getAction();
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            lastDownMouseKey = buttonState;
                            if (buttonState == MotionEvent.BUTTON_SECONDARY) {
                                // 左键
                                Log.i("gzw", "鼠标右键按下");
                            } else if (buttonState == MotionEvent.BUTTON_PRIMARY) {
                                // 右键
                                Log.i("gzw", "鼠标左键按下");
                            } else if (buttonState == MotionEvent.BUTTON_TERTIARY) {
                                // 鼠标中键
                                Log.i("gzw", "鼠标右键按下");
                            }
                            break;
                        case MotionEvent.ACTION_BUTTON_PRESS:
                            break;
                        case MotionEvent.ACTION_BUTTON_RELEASE:
                            break;
                        case MotionEvent.ACTION_UP:
                            if (lastDownMouseKey == MotionEvent.BUTTON_SECONDARY) {
                                // 左键
                                Log.i("gzw", "鼠标右键抬起");
                            } else if (lastDownMouseKey == MotionEvent.BUTTON_PRIMARY) {
                                // 右键
                                Log.i("gzw", "鼠标左键抬起");
                            } else if (lastDownMouseKey == MotionEvent.BUTTON_TERTIARY) {
                                // 鼠标中键
                                Log.i("gzw", "鼠标中键抬起");
                            }
                            lastDownMouseKey = 0;
                            break;
                        case MotionEvent.ACTION_SCROLL:
                            float axisValueY = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                            if (axisValueY == 1.0) {
                                // 鼠标滚轮向上滑动
                                Log.i("gzw", " 鼠标滚轮向上滑动");
                            } else if (axisValueY == -1.0) {
                                // 鼠标滚轮向下滑动
                                Log.i("gzw", " 鼠标滚轮向下滑动");
                            }
//                            float x = event.getX();
//                            float y = event.getY();
//                            float axisValueX = event.getAxisValue(event.AXIS_X);
//                            boolean fromSource = event.isFromSource(InputDevice.SOURCE_MOUSE);
//
//                            Log.i("gzw", "MotionEvent: " + event.toString() + " x: " + x + " y: " + y
//                                    + " axisValueX: " + axisValueX + " axisValueY: " + axisValueY + " fromSource: " + fromSource);


                            break;
                        case MotionEvent.ACTION_MOVE:
                            // 移动
                            float x = event.getX();
                            float y = event.getY();

                            Log.i("gzw", "鼠标移动 偏移值x: " + x + " 偏移值y: " + y);
                            break;
                    }


//                    switch (buttonState) {
//                        case MotionEvent.ACTION_DOWN:
//                            Log.i("gzw", "悬浮状态");
//                            break;
//                        case MotionEvent.BUTTON_PRIMARY:
//                            Log.i("gzw", "鼠标左键单击事件");
//
//                            break;
//                        case MotionEvent.BUTTON_TERTIARY:
//                            Log.i("gzw", "鼠标中键单击事件");
//
//                            break;
//                        case MotionEvent.BUTTON_SECONDARY:
//                            Log.i("gzw", "鼠标右键单击事件");
//                            break;
//                        default:
//                            Log.i("gzw", "default");
//
//                            break;
//                    }

                    return true;
                }
            });
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        Log.i("gzw", "onPointerCaptureChanged hasCapture: " + hasCapture);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        int keyCode1 = event.getKeyCode();
        int scanCode = event.getScanCode();
        int source = event.getSource();


        if (source == 8194 || source == 131076) {

        } else {
//            Log.i("gzw", "1111111 onKeyDown keyCode:" + keyCode
//                    + " keyCode1:" + keyCode1
//                    + " scanCode:" + scanCode
//                    + " source:" + source
//                    + " event:" + event.toString()
//            );
        }

        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        int keyCode1 = event.getKeyCode();
        int scanCode = event.getScanCode();
        int source = event.getSource();

        if (source == 8194 || source == 131076) {
            Log.i("gzw", "1111111 onKeyUp keyCode:" + keyCode
                    + " keyCode1:" + keyCode1
                    + " scanCode:" + scanCode
                    + " source:" + source
                    + " event:" + event.toString()
            );
        }


        return false;
    }
}

